
package com.banciyuan.bcywebview.base.helper.timelinedatacenter;

import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.utils.http.CommonApiUtils;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import org.json.JSONException;
import org.json.JSONObject;

import de.greenrobot.daoexample.model.DetailType;
import de.greenrobot.daoexample.model.Timeline;

/**
 * Created by hy on 2015/10/14.
 */
public class TimelineLikeHelper {

    private static boolean like_flag = false;
    private static boolean recommend_flag = false;

    public static void doLikeGroup(final Context mContext, RequestQueue mQueue, String post_id, String have_ding, final TimelineSuccess timelineSuccess) {
        if (like_flag) return;
        like_flag = true;
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    if (null != timelineSuccess)
                        timelineSuccess.onTimelineSuccess();
                } else {
                    doErrorResponse(response, mContext);
                }
                like_flag = false;
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                like_flag = false;
            }
        };
        if (!Boolean.parseBoolean(have_ding)) {
            if (!CommonApiUtils.doLikeGroup(mContext, mQueue, onSuccess, onError, post_id)) {
                like_flag = false;
            }
        } else {
            if (!CommonApiUtils.disLikeGroup(mContext, mQueue, onSuccess, onError, post_id)) {
                like_flag = false;
            }
        }
    }

    public static void doRecommendGroup(final Context mContext, RequestQueue mQueue, String post_id, String recommendStatus, final TimelineSuccess timelineSuccess) {
        if (recommend_flag) return;
        recommend_flag = true;
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    if (null != timelineSuccess)
                        timelineSuccess.onTimelineSuccess();
                } else {
                    doErrorResponse(response, mContext);
                }
                recommend_flag = false;
            }
        };
        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                recommend_flag = false;
            }
        };
        if (!Boolean.valueOf(recommendStatus)) {
            if (!CommonApiUtils.doRecommendGroup(mContext, mQueue, onSuccess, onError, post_id))
                recommend_flag = false;
        } else {
            if (!CommonApiUtils.disRecommendGroup(mContext, mQueue, onSuccess, onError, post_id))
                recommend_flag = false;
        }
    }

    public static void doRecommendAll(final Context mContext, RequestQueue mQueue, Timeline mTimeline, final TimelineSuccess timelineSuccess) {
        if (recommend_flag) return;
        recommend_flag = true;
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    if (null != timelineSuccess)
                        timelineSuccess.onTimelineSuccess();
                } else {
                    doErrorResponse(response, mContext);
                }
                recommend_flag = false;
            }
        };
        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                recommend_flag = false;
            }
        };
        if (!Boolean.valueOf(mTimeline.getHaveRecommend())) {
            if (!CommonApiUtils.doRecommend(mContext, mQueue, onSuccess, onError, mTimeline))
                recommend_flag = false;
        } else {
            if (!CommonApiUtils.disRecommend(mContext, mQueue, onSuccess, onError, mTimeline))
                recommend_flag = false;
        }
    }

    public static void doRecommendAll(final Context mContext, RequestQueue mQueue, DetailType detailType, String recommendStatus, final TimelineSuccess timelineSuccess) {
        if (recommend_flag) return;
        recommend_flag = true;
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    if (null != timelineSuccess)
                        timelineSuccess.onTimelineSuccess();
                } else {
                    doErrorResponse(response, mContext);
                }
                recommend_flag = false;
            }
        };
        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                recommend_flag = false;
            }
        };
        if (!Boolean.valueOf(recommendStatus)) {
            if (!CommonApiUtils.doRecommend(mContext, mQueue, onSuccess, onError, detailType))
                recommend_flag = false;
        } else {
            if (!CommonApiUtils.disRecommend(mContext, mQueue, onSuccess, onError, detailType))
                recommend_flag = false;
        }
    }

    public static void doRecommendGroupTeam(final Context mContext, RequestQueue mQueue, String gid, String recommendStatus, final TimelineSuccess timelineSuccess) {
        if (recommend_flag) return;
        recommend_flag = true;
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    if (null != timelineSuccess)
                        timelineSuccess.onTimelineSuccess();
                } else {
                    doErrorResponse(response, mContext);
                }
                recommend_flag = false;
            }
        };
        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                recommend_flag = false;
            }
        };
        if (!Boolean.valueOf(recommendStatus)) {
            if (!CommonApiUtils.doRecommendGroupTeam(mContext, mQueue, onSuccess, onError, gid))
                recommend_flag = false;
        } else {
            if (!CommonApiUtils.disRecommendGroupTeam(mContext, mQueue, onSuccess, onError, gid))
                recommend_flag = false;
        }
    }


    public static void doLikeAll(final Context mContext, RequestQueue mQueue, DetailType detailType, String likeStatus,  final TimelineSuccess timelineSuccess) {
        if (like_flag) return;
        like_flag = true;
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    if (null != timelineSuccess)
                        timelineSuccess.onTimelineSuccess();
                } else {
                    doErrorResponse(response, mContext);
                }
                like_flag = false;
            }
        };
        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                like_flag = false;
            }
        };
        if (Boolean.parseBoolean(likeStatus)) {
            if (CommonApiUtils.dislike(mContext, mQueue, onSuccess, onError, detailType)) {
                like_flag = false;
            }
        } else {
            if (!CommonApiUtils.doLike(mContext, mQueue, onSuccess, onError, detailType)) {
                like_flag = false;
            }
        }
    }

    public static void doLikeAll(final Context mContext, RequestQueue mQueue, Timeline mTimeline, final TimelineSuccess timelineSuccess) {
        if (like_flag) return;
        like_flag = true;
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    if (null != timelineSuccess)
                        timelineSuccess.onTimelineSuccess();
                } else {
                    doErrorResponse(response, mContext);
                }
                like_flag = false;
            }
        };
        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                like_flag = false;
            }
        };
        if (Boolean.parseBoolean(mTimeline.getHaveDing())) {
            if (CommonApiUtils.dislike(mContext, mQueue, onSuccess, onError, mTimeline)) {
                like_flag = false;
            }
        } else {
            if (!CommonApiUtils.doLike(mContext, mQueue, onSuccess, onError, mTimeline)) {
                like_flag = false;
            }
        }
    }

    public static void doLikeAllForCircle(final Context mContext, RequestQueue mQueue, Timeline mTimeline, final TimelineSuccess timelineSuccess) {
        if (like_flag) return;
        like_flag = true;
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    if (null != timelineSuccess)
                        timelineSuccess.onTimelineSuccess();
                } else {
                    doErrorResponse(response, mContext);
                }
                like_flag = false;
            }
        };
        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                like_flag = false;
            }
        };
        if (mTimeline.isHave_ding()) {
            if (CommonApiUtils.dislike(mContext, mQueue, onSuccess, onError, mTimeline)) {
                like_flag = false;
            }
        } else {
            if (!CommonApiUtils.doLike(mContext, mQueue, onSuccess, onError, mTimeline)) {
                like_flag = false;
            }
        }
    }

    private static void doErrorResponse(String response, Context mContext) {
        JSONObject result = null;
        try {
            result = new JSONObject(response);
            String data = result.getString(HttpUtils.UPLOAD_DATA);
            MyToast.show(mContext, data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static void doFollow(final Context context, String type, String uid, final FollowSuccess followSuccess) {
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, context)) {
                    if (null != followSuccess)
                        followSuccess.onFollowSuccess(response);
                } else {
                    doErrorResponse(response, context);
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        };
        CommonApiUtils.doFollow(context, VolleyQueue.getRquest(context), onSucc, onError, uid, type);
    }

    public static void removeFans(final Context context, String uid, final FollowSuccess followSuccess) {
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    if (StatusCodeUtils.dealStatusCode(response, context)) {
                        if (null != followSuccess) {
                            JSONObject jsonObject = new JSONObject(response);
                            followSuccess.onFollowSuccess(jsonObject.getString(HttpUtils.UPLOAD_DATA));
                        }
                    } else {
                        doErrorResponse(response, context);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        };
        CommonApiUtils.removeFans(context, VolleyQueue.getRquest(context), onSucc, onError, uid);
    }

    public interface TimelineSuccess {
        void onTimelineSuccess();
    }

    public interface FollowSuccess {
        void onFollowSuccess(String response);
    }
}
